Skip to content

refactor: streamline dependencies and add tests - #1

Merged
mingcheng merged 1 commit into
mainfrom
chore/optimize-the-project-and-add-tests
Jul 23, 2026
Merged

mingcheng merged 1 commit into
mainfrom
chore/optimize-the-project-and-add-tests

Conversation

@mingcheng

@mingcheng mingcheng commented Jul 23, 2026 •

Copy link
Copy Markdown
Owner
  • remove inline package installation and loading from R modules
  • use fully qualified function names and native pipe syntax
  • add helpers.R with centralized visualization font setup
  • introduce run_tests.R with coverage for stats and charts
  • update CI package list and README requirements

Summary by Sourcery

Centralize dependency management and visualization font handling while adding basic test coverage for statistics and chart generation.

New Features:

  • Introduce helpers.R to centralize visualization font setup for pie charts and treemaps.
  • Add tests/run_tests.R to exercise repository statistics, font setup, and chart file generation.

Enhancements:

  • Remove inline package installation and loading from R modules in favor of upfront dependency checks and namespaced calls.
  • Extend main script to source shared helpers and all visualization modules through a common loader.
  • Update plotting code to use native R pipe syntax and fully qualified dplyr and ggplot2 calls.
  • Simplify fmt.R to fail fast when styler is missing instead of installing it implicitly.

CI:

  • Update GitHub Actions workflow to install the revised package set and run the new R tests before the analysis job.

Documentation:

  • Document test execution in the README and clarify required R packages with an example install command.

Tests:

  • Add high-level tests that verify R files parse, repository statistics are aggregated correctly, fonts are configured as expected, and chart output files are created.

Summary by CodeRabbit

  • New Features

    • Added automated validation for data processing, font handling, and chart generation.
    • Added support for configuring a custom visualization font with a fallback when unavailable.
    • Added date-based filenames for generated pie charts.
  • Bug Fixes

    • Improved handling of missing dependencies and unavailable fonts.
    • Improved cached data loading and proxy configuration behavior.
  • Documentation

    • Added instructions for installing required packages and running tests.

- remove inline package installation and loading from R modules
- use fully qualified function names and native pipe syntax
- add helpers.R with centralized visualization font setup
- introduce run_tests.R with coverage for stats and charts
- update CI package list and README requirements

Signed-off-by: mingcheng <mingcheng@apache.org>
@mingcheng
mingcheng requested a review from Copilot July 23, 2026 03:15
@sourcery-ai

sourcery-ai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Centralizes dependency management and visualization font setup, replaces inline package loading with namespace-qualified calls and native pipes, and adds a small test harness plus CI integration to validate stats aggregation and chart generation.

Sequence diagram for shared visualization font setup

sequenceDiagram
  participant main_R as main.R
  participant helpers_R as helpers.R
  participant pie_R as pie.R
  participant treemap_R as treemap.R

  main_R->>helpers_R: source_module R/helpers.R
  main_R->>pie_R: source_module R/pie.R
  main_R->>treemap_R: source_module R/treemap.R

  main_R->>pie_R: generate_pie_chart(username, repos, dir_name, json_data)
  pie_R->>helpers_R: setup_visualization_font(font_path)
  helpers_R-->>pie_R: font_family
  pie_R->>pie_R: generate_chart(..., family = font_family)

  main_R->>treemap_R: generate_treemap(username, repos, dir_name, json_data)
  treemap_R->>helpers_R: setup_visualization_font(font_path)
  helpers_R-->>treemap_R: font_family
  treemap_R->>treemap_R: ggplot2::ggplot(..., theme_minimal(base_family = font_family))
Loading

Flow diagram for centralized package checking and module sourcing

flowchart TD
  A[main.R start] --> B[Define required_packages]
  B --> C[Compute missing_packages with requireNamespace]
  C -->|missing_packages length > 0| D[stop with install.packages message]
  C -->|no missing packages| E[source_module]
  E --> F[lapply over R/helpers.R, R/fetch.R, R/plot.R, R/pie.R, R/treemap.R]
  F --> G[fetch_json and plot_repos]
  G --> H[generate_pie_chart]
  G --> I[generate_treemap]
  H --> J[Pie chart files]
  I --> K[Treemap files]
  J --> L[Analysis complete]
  K --> L
Loading

File-Level Changes

Change Details Files
Centralize package management and module sourcing in main entrypoint.
  • Replace inline installation of jsonlite with upfront required_packages and missing_packages check that stops on missing dependencies.
  • Introduce source_module helper with error handling, and use it via lapply to source helpers.R, fetch.R, plot.R, pie.R, and treemap.R.
  • Switch proxy environment handling to use nzchar and qualify jsonlite fromJSON/write_json calls.
R/main.R
Use fully qualified dplyr verbs and native pipe syntax in statistics pipeline.
  • Remove local package loading for dplyr/magrittr.
  • Convert %>% chains to native
> pipe.
  • Qualify mutate, group_by, summarise, filter, arrange, desc, and n with dplyr::, keeping the overall aggregation logic intact.
  • Refactor HTTP and JSON utilities to avoid inline package installation and rely on namespaces.
    • Remove runtime installation and library() calls for httr/jsonlite.
    • Qualify GET, add_headers, use_proxy, status_code, content, and fromJSON calls with httr:: and jsonlite:: in fetch_json.
    R/fetch.R
    Centralize visualization font setup and apply it consistently to treemap and pie charts.
    • Extract font setup into setup_visualization_font helper that loads FiraCode via sysfonts, enables showtext, and falls back to sans with warnings when font is missing or fails.
    • Update generate_treemap to call setup_visualization_font, use the returned font family in treemap text and theme, and qualify ggplot2/treemapify APIs and ggsave calls.
    • Update generate_pie_chart to use setup_visualization_font for device family, add today stamp for filenames, and remove inline showtext/font_add setup.
    R/helpers.R
    R/treemap.R
    R/pie.R
    Tighten fmt tooling dependency behavior.
    • Replace dynamic installation and library loading of styler with a requireNamespace check that stops with an explicit message when styler is missing.
    R/fmt.R
    Introduce a lightweight test harness for stats and chart generation and integrate it into documentation and CI.
    • Add tests/run_tests.R that sources core R modules, defines a run_test helper, and executes tests for parsing, stats aggregation invariants, font behavior, and output file generation.
    • Wire tests/run_tests.R into README with a Tests section and into GitHub Actions workflow, including tests/** in paths and running tests before analysis.
    • Update CI package list to match runtime needs (add sysfonts, drop magrittr) and adjust install.packages dependencies argument to NA.
    • Document required R packages and provide an install.packages snippet aligned with CI configuration.
    tests/run_tests.R
    README.md
    .github/workflows/r.yml

    Tips and commands

    Interacting with Sourcery

    • Trigger a new review: Comment @sourcery-ai review on the pull request.
    • Continue discussions: Reply directly to Sourcery's review comments.
    • Generate a GitHub issue from a review comment: Ask Sourcery to create an
      issue from a review comment by replying to it. You can also reply to a
      review comment with @sourcery-ai issue to create an issue from it.
    • Generate a pull request title: Write @sourcery-ai anywhere in the pull
      request title to generate a title at any time. You can also comment
      @sourcery-ai title on the pull request to (re-)generate the title at any time.
    • Generate a pull request summary: Write @sourcery-ai summary anywhere in
      the pull request body to generate a PR summary at any time exactly where you
      want it. You can also comment @sourcery-ai summary on the pull request to
      (re-)generate the summary at any time.
    • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
      request to (re-)generate the reviewer's guide at any time.
    • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
      pull request to resolve all Sourcery comments. Useful if you've already
      addressed all the comments and don't want to see them anymore.
    • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
      request to dismiss all existing Sourcery reviews. Especially useful if you
      want to start fresh with a new review - don't forget to comment
      @sourcery-ai review to trigger a new review!

    Customizing Your Experience

    Access your dashboard to:

    • Enable or disable review features such as the Sourcery-generated pull request
      summary, the reviewer's guide, and others.
    • Change the review language.
    • Add, remove or edit custom review instructions.
    • Adjust other review settings.

    Getting Help

    @coderabbitai

    coderabbitai Bot commented Jul 23, 2026 •

    Copy link
    Copy Markdown

    Review Change Stack

    📝 Walkthrough

    Walkthrough

    The PR centralizes R dependency and module loading, adds shared visualization font handling, updates plotting code to use explicit namespaces, introduces chart and aggregation tests, and integrates test execution into CI and README instructions.

    Changes

    R workflow and visualization updates

    Layer / File(s) Summary
    Dependency checks and module loading
    .github/workflows/r.yml, R/fetch.R, R/fmt.R, R/main.R, R/plot.R
    Runtime dependencies and module sourcing are centralized, package calls are namespace-qualified, and plotting pipelines use native pipes with qualified dplyr verbs.
    Shared visualization font handling
    R/helpers.R, R/pie.R, R/treemap.R
    Chart generation uses shared FiraCode setup with a sans fallback, explicit plotting namespaces, and date-stamped pie chart filenames.
    Automated tests and CI validation
    tests/run_tests.R, .github/workflows/r.yml
    Tests validate parsing, aggregation, font fallback, and generated SVG/PNG outputs, and CI runs the test runner.
    Dependency and test documentation
    README.md
    Requirements and package installation instructions are updated, with a command for running tests.

    Estimated code review effort: 3 (Moderate) | ~25 minutes

    🚥 Pre-merge checks | ✅ 5
    ✅ Passed checks (5 passed)
    Check name Status Explanation
    Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
    Title check ✅ Passed The title accurately summarizes the main changes: dependency cleanup and added tests.
    Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
    Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
    Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
    ✨ Finishing Touches
    🧪 Generate unit tests (beta)
    • Create PR with unit tests
    • Commit unit tests in branch chore/optimize-the-project-and-add-tests

    Warning

    There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

    🔧 Checkov (3.3.8)
    .github/workflows/r.yml

    Traceback (most recent call last):
    File "/usr/local/bin/checkov", line 2, in
    from checkov.main import Checkov
    ModuleNotFoundError: No module named 'checkov'


    Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

    ❤️ Share

    Comment @coderabbitai help to get the list of available commands.

    @sourcery-ai sourcery-ai Bot left a comment

    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Hey - I've reviewed your changes and they look great!


    Sourcery is free for open source - if you like our reviews please consider sharing them ✨
    Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

    Copilot AI left a comment

    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Pull request overview

    This PR refactors the R modules to remove inline package installation/loading, centralizes visualization font setup, and adds a lightweight Rscript-based test runner that is executed in CI.

    Changes:

    • Centralize custom font registration and showtext activation in R/helpers.R, and update chart modules to use it.
    • Refactor R modules to use namespaced package calls and the native pipe operator (|>), with dependency checks moved to R/main.R.
    • Add tests/run_tests.R, wire it into GitHub Actions, and document test execution in the README.

    Reviewed changes

    Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

    Show a summary per file
    File Description
    tests/run_tests.R Adds a simple test runner that parses R files, validates aggregation output, and checks chart output generation.
    README.md Documents test execution and updates the required package list / install command.
    R/treemap.R Removes inline package management, uses namespaced ggplot2/treemapify calls, and uses centralized font setup.
    R/plot.R Removes inline package management and switches dplyr pipeline to native `
    R/pie.R Removes inline font/package setup and uses centralized font setup; standardizes output naming using today.
    R/main.R Adds upfront required-package validation and a common module loader; uses namespaced jsonlite functions.
    R/helpers.R Introduces a shared helper to register the bundled font and enable showtext with fallback behavior.
    R/fmt.R Changes formatting script to fail fast when styler is missing (no implicit install).
    R/fetch.R Removes inline installs/library calls and uses fully qualified httr/jsonlite calls.
    .github/workflows/r.yml Updates package list, adjusts install dependencies mode, and runs the new tests before analysis.

    💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

    Comment thread README.md
    Comment on lines 69 to +70
    - R >= 4 (tested on 4.5.1)
    - R packages: `httr`, `jsonlite`, `dplyr`, `magrittr`, `showtext`, `ggplot2`, `treemapify`, `svglite`
    - R packages: `httr`, `jsonlite`, `dplyr`, `showtext`, `sysfonts`, `ggplot2`, `treemapify`, `svglite`

    @coderabbitai coderabbitai Bot left a comment

    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Actionable comments posted: 2

    🤖 Prompt for all review comments with AI agents
    Verify each finding against current code. Fix only still-valid issues, skip the
    rest with a brief reason, keep changes minimal, and validate.
    
    Inline comments:
    In @.github/workflows/r.yml:
    - Line 36: Update the path filters in the workflow so changes under assets/**,
    including assets/FiraCode.ttf, trigger the test suite alongside the existing R
    and tests/** patterns.
    
    In `@README.md`:
    - Around line 70-74: Update the documented minimum R version in README.md from R
    >= 4 to R >= 4.1, ensuring the installation requirements accurately reflect the
    native |> syntax used by R/plot.R.
    
    🪄 Autofix (Beta)

    Fix all unresolved CodeRabbit comments on this PR:

    • Push a commit to this branch (recommended)
    • Create a new PR with the fixes

    ℹ️ Review info
    ⚙️ Run configuration

    Configuration used: defaults

    Review profile: CHILL

    Plan: Pro Plus

    Run ID: 9dbd77c6-ffb5-4db4-8c8c-e1a83c2d8b61

    📥 Commits

    Reviewing files that changed from the base of the PR and between 02a0e0e and 4446bef.

    📒 Files selected for processing (10)
    • .github/workflows/r.yml
    • R/fetch.R
    • R/fmt.R
    • R/helpers.R
    • R/main.R
    • R/pie.R
    • R/plot.R
    • R/treemap.R
    • README.md
    • tests/run_tests.R

    Comment thread .github/workflows/r.yml
    - main
    paths:
    - "R/**"
    - "tests/**"

    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

    Trigger validation when the bundled font changes.

    The test suite depends on assets/FiraCode.ttf, but this path filter covers R and test files only. Add assets/** so a removed or invalid font runs CI.

    Proposed fix
           - "tests/**"
    +      - "assets/**"
    📝 Committable suggestion

    ‼️ IMPORTANT
    Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    Suggested change
    - "tests/**"
    - "tests/**"
    - "assets/**"
    🤖 Prompt for AI Agents
    Verify each finding against current code. Fix only still-valid issues, skip the
    rest with a brief reason, keep changes minimal, and validate.
    
    In @.github/workflows/r.yml at line 36, Update the path filters in the workflow
    so changes under assets/**, including assets/FiraCode.ttf, trigger the test
    suite alongside the existing R and tests/** patterns.
    

    Comment thread README.md
    Comment on lines +70 to +74
    - R packages: `httr`, `jsonlite`, `dplyr`, `showtext`, `sysfonts`, `ggplot2`, `treemapify`, `svglite`

    ```r
    install.packages(c("httr", "jsonlite", "dplyr", "showtext", "sysfonts", "ggplot2", "treemapify", "svglite"))
    ```

    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

    🧩 Analysis chain

    🏁 Script executed:

    rg -n '\|>' R
    rg -n 'R (>=|version)|r-version|R_VERSION' README.md .github

    Repository: mingcheng/langstat

    Length of output: 489


    🌐 Web query:

    R 4.0.0 release notes native pipe |><- operator introduced version

    💡 Result:

    The native pipe operator |> was not introduced in R 4.0.0; it was introduced in R 4.1.0 [1][2][3][4]. While R 4.0.0 included various updates and changes, it did not include the |> operator [5]. The native forward pipe syntax |> was officially added in R 4.1.0, allowing for the piping of the left-hand side expression as the first argument into the right-hand side call [3][6][7]. Subsequent versions of R expanded the functionality of the native pipe, such as the addition of the _ placeholder in R 4.2.0 [6].

    Citations:


    Raise the minimum documented R version to 4.1.

    The project uses native |> syntax in R/plot.R, which R 4.0 cannot parse. Update README.md from R >= 4 to R >= 4.1 before users on the documented minimum try to install/run the package.

    🤖 Prompt for AI Agents
    Verify each finding against current code. Fix only still-valid issues, skip the
    rest with a brief reason, keep changes minimal, and validate.
    
    In `@README.md` around lines 70 - 74, Update the documented minimum R version in
    README.md from R >= 4 to R >= 4.1, ensuring the installation requirements
    accurately reflect the native |> syntax used by R/plot.R.
    

    Repository owner deleted a comment from gemini-code-assist Bot Jul 23, 2026
    @mingcheng
    mingcheng merged commit 42ea75f into main Jul 23, 2026
    3 checks passed
    @mingcheng
    mingcheng deleted the chore/optimize-the-project-and-add-tests branch July 23, 2026 03:24
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    None yet

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants